﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using CNdotnet.Ezsocio.BusinessObjects;

namespace CNdotnet.Ezsocio.BusinessServices
{
    /// <summary>
    /// UCenter DataContext in service layer
    /// </summary>
    public class ContextService
    {
        private static ContextService instance = null;

        private ContextService() { }

        public static ContextService Instance()
        {
            if (instance == null)
                instance = new ContextService();

            return instance;
        }

        public static ContextService Current
        {
            get { return Instance(); }
        }

        public static void Close()
        {
            if (instance==null) {
                return;
            }
            if (instance.User != null)
            {
                new AccountService().UpdateOnlie(ContextService.Current.User.ID, false);
                instance.User = null;            
            }

            if (instance.App != null)
            {
                instance.App = null;
            }
        }

        public AccountContract User
        {
            get { return HttpContext.Current.Session["CurrentUser"] as AccountContract; }
            set { HttpContext.Current.Session["CurrentUser"] = value; }
        }

        public AppContract App
        {
            get { return HttpContext.Current.Session["CurrentApp"] as AppContract; }
            set { HttpContext.Current.Session["CurrentApp"] = value; }
        }
    }

}
